package com.jspgou.cms.dao.impl;

import java.util.Date;
import java.util.Iterator;
import java.util.List;

import org.apache.commons.lang.StringUtils;
import org.hibernate.Query;
import org.springframework.stereotype.Repository;

import com.jspgou.common.hibernate3.Finder;
import com.jspgou.common.hibernate3.HibernateBaseDao;
import com.jspgou.common.page.Pagination;
import com.jspgou.cms.dao.ConsultDao;
import com.jspgou.cms.entity.Consult;

@Repository
public class ConsultDaoImpl extends HibernateBaseDao<Consult, Long> implements
		ConsultDao {
	public Consult findById(Long id) {
		Consult entity = get(id);
		return entity;
	}

	public Consult saveOrUpdate(Consult bean) {
		getSession().saveOrUpdate(bean);
		return bean;
	}
	@SuppressWarnings("unchecked")
	public Consult getSameConsult(Long memberId) {
		 Iterator<Consult> it=this.getSession().createQuery("from Consult bean where bean.member.id=:id order by bean.id desc")
		.setParameter("id", memberId).setMaxResults(1).iterate();
		 if(it.hasNext()){    //只返回该会员对该商品的最新评论
			 return it.next();
		 }else{
			 return null;
		 }
	}
	
	
	//更改方法，分类显示咨询。
	public Pagination getPage(Long productId,Long memberId,Long storeId,String consultType,String userName,String productName,
			Date startTime,Date endTime,int pageNo,int pageSize,boolean cache){
		Finder f=Finder.create("from Consult bean where 1=1 ");
	    if(productId!=null){
	    	f.append(" and bean.product.id=:productId");
	    	f.setParam("productId", productId);
	    }
	    if(memberId!=null){
	    	f.append(" and bean.member.id=:memberId");
	    	f.setParam("memberId", memberId);
	    }
	    if(storeId!=null){
	    	f.append(" and bean.store.id=:storeId");
	    	f.setParam("storeId", storeId);
	    }
	    
	    //如果咨询类型不为空，追加查询条件
	    if(consultType!=null){
	    	f.append(" and bean.consultType=:consultType");
	    	f.setParam("consultType", consultType);
	    }
		if (!StringUtils.isBlank(userName)) {
			f.append(" and bean.member.member.user.username like:userName");
			f.setParam("userName", "%"+userName+"%");
		}
	   if (!StringUtils.isBlank(productName)) {
		f.append(" and bean.product.name like:productName");
		f.setParam("productName", "%"+productName+"%");
	   }
	   if(startTime!=null){
		f.append(" and bean.consultTime>:startTime");
		f.setParam("startTime", startTime);
	  }
	  if(endTime!=null){
		f.append(" and bean.consultTime<:endTime");
		f.setParam("endTime", endTime);
	  }
	    f.append(" order by bean.id desc");
		return find(f, pageNo, pageSize);
	}
	
	//会员中心的查咨询
	public Pagination getPage(Long memberId,Long storeId,int pageNo,int pageSize,boolean cache){
		Finder f=Finder.create("from Consult bean");
	    if(memberId!=null){
	    	f.append(" where bean.member.id=:memberId");
	    	f.setParam("memberId", memberId);
	    }
	    if(storeId!=null){
	    	f.append(" where bean.store.id=:storeId");
	    	f.setParam("storeId", storeId);
	    }
	    f.append(" order by bean.id desc");
		return find(f, pageNo, pageSize);
	}

	public Consult update(Consult bean) {
		getSession().update(bean);
		return bean;
	}

	public Consult deleteById(Long id) {
		Consult entity = super.get(id);
		if (entity != null) {
			getSession().delete(entity);
		}
		return entity;
	}

	//添加方法，根据咨询类型删除记录 
	public List<Consult> deleteByType(String consultType) {
		List<Consult> consultList=findByType(null,consultType);
		if(consultList!=null){
			for(Consult consult:consultList){
				getSession().delete(consult);
		    }
		}
		return consultList;
	}
	//添加方法，根据咨询类型查找consult
	public 	List<Consult> findByType(Long productId,String consultType){
		Finder finder=Finder.create("from Consult bean where 1=1");
		if(productId!=null){
			finder.append(" and bean.product.id=:productId");
			finder.setParam("productId", productId);
		}
		if(consultType!=null){
			finder.append(" and bean.consultType=:consultType");
			finder.setParam("consultType", consultType);
		}
		return find(finder);
	}
	
	protected Class<Consult> getEntityClass() {
		return Consult.class;
	}


}